package com.maple.sharding.controller;

import com.maple.sharding.dao.AccountProfileDAO;
import com.maple.sharding.dao.FinancialOrderDAO;
import com.maple.sharding.entity.AccountProfileDO;
import com.maple.sharding.entity.FinancialOrderDO;
import com.maple.sharding.util.SnowFlake;
import com.maple.sharding.util.Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
public class TestController {

    @Autowired
    private AccountProfileDAO accountProfileDAO;

    @Autowired
    private FinancialOrderDAO financialOrderDAO;

    @Autowired
    private SnowFlake snowFlake;

    @GetMapping("order/insert")
    public FinancialOrderDO orderInsert() {

        FinancialOrderDO financialOrderDO = new FinancialOrderDO();

        String userId = "123456789012";
        String orderId = "947511521019";

        financialOrderDO.setUserId(userId);
        financialOrderDO.setOrderId(orderId);
        financialOrderDO.setOrderAmount("999");

        financialOrderDAO.insert(financialOrderDO);
        System.out.println("新增成功" + financialOrderDO);

        FinancialOrderDO financialOrderByOrderId = financialOrderDAO.selectByOrderId(orderId);
        System.out.println("查询orderId查询成功" + financialOrderByOrderId);

        FinancialOrderDO financialOrderByUserId = financialOrderDAO.selectByUserId(userId);
        System.out.println("查询userId查询成功" + financialOrderByUserId);

        FinancialOrderDO financialOrderByUserIdAndOrderId = financialOrderDAO.selectByUserIdAndOrderId(userId, orderId);
        System.out.println("查询userId 和 orderId 查询成功" + financialOrderByUserIdAndOrderId);

        return financialOrderDO;
    }

    @GetMapping("/insert")
    public AccountProfileDO insert() {

        // 新增accountProfile
        AccountProfileDO accountProfileDO = new AccountProfileDO();

        //7977551913518369620
        String userId = snowFlake.getId();

        accountProfileDO.setUserId(userId);
        accountProfileDO.setUserName("maple");

        accountProfileDAO.insert(accountProfileDO);
        System.out.println("新增成功" + accountProfileDO);

        AccountProfileDO accountProfileDOByUserId = accountProfileDAO.selectByUserId(userId);
        System.out.println("查询userId查询成功" + accountProfileDOByUserId);

        // 新增financialOrder

        FinancialOrderDO financialOrderDO = new FinancialOrderDO();

        String orderId = Utils.genUUID(16) + StringUtils.substring(userId, userId.length() - 3);

        financialOrderDO.setUserId(userId);
        financialOrderDO.setOrderId(orderId);
        financialOrderDO.setOrderAmount(Utils.genUUID(3));
        financialOrderDAO.insert(financialOrderDO);
        System.out.println("新增成功" + financialOrderDO);

        FinancialOrderDO financialOrderByOrderId = financialOrderDAO.selectByOrderId(orderId);
        System.out.println("查询orderId查询成功" + financialOrderByOrderId);

        FinancialOrderDO financialOrderByUserId = financialOrderDAO.selectByUserId(userId);
        System.out.println("查询userId查询成功" + financialOrderByUserId);

        FinancialOrderDO financialOrderByUserIdAndOrderId = financialOrderDAO.selectByUserIdAndOrderId(userId, orderId);
        System.out.println("查询userId 和 orderId 查询成功" + financialOrderByUserIdAndOrderId);

        return accountProfileDO;
    }

    /*@GetMapping("/user/insert")
    public AccountProfileDO userInsert() {

        FinancialOrderDO financialOrderDO = new FinancialOrderDO();
        financialOrderDO.setOrderId(100L);
        financialOrderDO.setUserId(552506879817809920L);
        financialOrderDO.setOrderAmount("300");

        financialOrderDAO.insert(financialOrderDO);
        System.out.println("新增成功" + financialOrderDO);
        AccountProfileDO accountProfileDO = new AccountProfileDO();
        accountProfileDO.setUserName("maple");
        accountProfileDAO.insert(accountProfileDO);

        System.out.println("新增成功" + accountProfileDO);

        return accountProfileDO;
    }

    @GetMapping("/user/query")
    public AccountProfileDO userQuery(String id) {

        AccountProfileDO accountProfileDO = accountProfileDAO.selectByUserId(Long.valueOf(id));

        System.out.println("查询成功" + accountProfileDO);

        *//*FinancialOrderDO financialOrderDO = financialOrderDAO.selectByOrderId(Long.valueOf(orderId));

        System.out.println("查询成功" + financialOrderDO);*//*

        return accountProfileDO;
    }*/


    /*@GetMapping("/order/query")
    public FinancialOrderDO orderQuery(String id) {

        FinancialOrderDO financialOrderDO = financialOrderDAO.selectByOrderId(Long.valueOf(id));

        //FinancialOrderDO financialOrderDO = financialOrderDAO.selectByUserId(Long.valueOf(id));

        System.out.println("查询成功" + financialOrderDO);

        return financialOrderDO;
    }

    public static void main(String[] args) {
        System.out.println(5 % 3);
    }*/
}
